package model.area_effect;

import model.core.GameScheduler;
import model.core.Notifiable;
import model.effects.Effect;
import model.entity.Entity;
import model.map.MapPoint;

/**
 * These are general area effects used on the game map.
 * @author Zachary
 */
public class MapAreaOfEffect extends AreaOfEffect implements Notifiable, Cloneable{
	private int period;
	private boolean isDelayed;
	private int numHits;

	public MapAreaOfEffect(String name, String graphicID, Effect effect, int period, int numHits) {
		super(name, graphicID, effect);
		this.period = period;
		this.isDelayed = false;
		this.numHits = numHits;
	}
	
	/**
	 * When exectued, the MapAreaOfEffect is no longer delayed
	 * and can once again affect entities.
	 */
	public void execute() {
		this.isDelayed = false;
	}

	/**
	 * Applies the AoE's effect to the entity if it is
	 * not delayed.  If the number of hits remaining
	 * for that AoE is zero, remove it from the map.
	 */
	public void interact(Entity entity) {
		if(!isDelayed){
			this.getEffect().doEffect(entity);
			if(--this.numHits == 0){
				MapPoint aoeLocation = entity.getLocation();
				AreaEffectRemover.getInstance().removeMapAreaEffectFromMap(this, aoeLocation);
			}
			GameScheduler.getInstance().registerOnce(this, period);
			this.isDelayed = true;
		}
	}

	@Override
	public MapAreaOfEffect clone() throws CloneNotSupportedException {
		return (MapAreaOfEffect)super.clone();
	}
}
